@extends('layouts.default')

@section('title', '班队会')

@push('scripts')
<script src="{{ asset('lib/xm-select-v1.2.4/xm-select.js')}}"></script>
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage','upload'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var upload = layui.upload;
        var xmDemo;
        var PageId = "{{request()->get('id',0)}}";
        
        renderSelect();

        laydate.render({
            elem: '#show-date'
            ,type: 'date'
        });

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            await renderParticipants();
            pageInit();
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['LEVEL', 'GRADE', 'reception_class', 'classItem']}, function (res){
                    if(res.code === 1) {  
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];
                            
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }
                        
                        form.render(); 
                        
                        resolve();
                    }
                });
            });
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                    
                        form.render('select');

                        resolve();
                    }
                });  
            });
        } 

        //参与教师下拉框数据渲染
        function renderParticipants() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        xmDemo = xmSelect.render({
                            el: '#pter', 
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },   
                            direction: 'down',           
                            filterable: true,
                            height: 'auto',
                            name:'participant',
                            layVerify: 'required',
                            layVerType: 'msg',
                            layReqText:'参与教师不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            model: {
                                label: {
                                    type: 'block',
                                    block: {
                                        //最大显示数量, 0:不限制
                                        showCount: 0,
                                        //是否显示删除图标
                                        showIcon: false,
                                    }
                                }
                            },
                            disabled:true,
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        })

                        return resolve();
                    }
                });  
            });
        }

        // 文件显示
        function fileShow(files) {
            let showFiles = {};
            layui.each(files, function(i, d) {
                if(d.field) {
                    if (showFiles[d.field]) {
                        showFiles[d.field].push(d);
                    } else {
                        showFiles[d.field] = [d];
                    }
                } else {
                    if (showFiles['zmd']) {
                        showFiles['zmd'].push(d);
                    } else {
                        showFiles['zmd'] = [d];
                    }
                }
            });

            layui.each(showFiles, function(f, d) {
                let fileShowTpl = fileShowDemo.innerHTML
                    ,fileShow = document.getElementById('file-show-'+f);
                    
                laytpl(fileShowTpl).render(d, function(html){
                    fileShow.innerHTML = html;                           
                });
            });
        }

        //数据渲染
        function pageInit(){
            getActivityOne({id:PageId}, function(res) {
                if(res.code === 1) {
                    let Activity = res.data;

                    form.val('infoForm', {
                        'id': PageId,
                        'data_version_id': Activity.data_version_id,
                        'name': Activity.name,
                        'date': Activity.date,
                        'level': Activity.level_array[0].optionId,
                        'grade': Activity.grade_array[0].optionId,
                        'class_item': Activity.class_item_set[0].optionId,
                        'reception_class': Activity.reception_class_set[0].optionId,
                        'created_at': Activity.created_at
                    });
                    form.val('checkForm', {
                        'form_id': Activity.id,
                        'form_instance_id': Activity.form_instance_id,
                        'status': Activity.instance.status,
                        'comment': Activity.instance.comment
                    });

                    // 创建人
                    if(Activity.creator){
                        var creatorName = '<button class="layui-btn layui-btn-primary" type="button">' + Activity.creator.nickname + '</button>';
                        
                        $("#creator-show").append(creatorName);
                    }

                    //参与教师
                    xmDemo.setValue(Activity.participants_users.pant);
                    //文件  
                    if(Activity.proof_material_array.length){
                        fileShow(Activity.proof_material_array);
                    }
                } else {
                    layer.msg('无数据记录', {time: 2000, icon: 5});
                }
            });
        }

        //提交
        form.on('submit(sub)', function(data){
            let d = data.field;

            checkActivity(d, function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });

            return false;
        });

    });
</script>
@endpush

@section('content')
<style>
    .layui-row{margin-bottom: 10px;}
    .opt-panel{padding: 10px;}
    .panel-title h3{margin-bottom: 20px;font-weight: 800;}

    .opt-form{padding: 20px 5px;}
    .opt-area{display: flex;flex-direction: row-reverse;padding: 10px 20px;margin-bottom: 20px;}
    .opt-area button, .back-btn{margin-left: 10px;}
</style>

<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="info-form" lay-filter="infoForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>班队会 - 查看</h3>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>学年学期：</label>
                        <div class="layuik-input-inline">
                            <select disabled class="data-version-id" name="data_version_id" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>名称：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="name" required lay-verify="required" placeholder="请输入" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div> 
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>日期：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="date" id="show-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>级别：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="level" lay-filter="levelList" data-role="levelList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div> 
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>年级：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="grade" lay-filter="gradeList" data-role="gradeList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>班级：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="class_item" lay-filter="classitemList" data-role="classitemList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div> 
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>接待课：</label>
                        <div class="layuik-input-inline">
                            <select disabled name="reception_class" lay-filter="reception_classList" data-role="reception_classList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>参与教师：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="pter" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>

                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">证明单：</label>
                        <div class="layuik-input-inline">
                            <div id="file-show-zmd"></div>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">其他附件：</label>
                        <div class="layuik-input-inline">
                            <div id="file-show-ja"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">课件：</label>
                        <div class="layuik-input-inline">
                            <div id="file-show-kj"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">创建人：</label>
                        <div class="layuik-input-inline" id="creator-show"></div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label">创建时间：</label>
                        <div class="layuik-input-inline">
                            <input disabled type="text" name="created_at" id="created_at" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>   
</div>
  
  
<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="check-form" lay-filter="checkForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>班队会 - 审核</h3>
                </div>
                
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>审核结果：</label>
                        <div class="layuik-input-inline">
                            <input  type="radio" name="status" value="PASS" title="通过">
                            <input  type="radio" name="status" value="REVIEW_FAIL" title="驳回" >
                        </div>
                    </div>
                </div> 
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">原因：</label>
                        <div class="layuik-input-inline">
                            <textarea  name="comment" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div> 
            </div>
            
            <div class="layui-row">
                <div class="opt-area">
                    <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary back-btn">返回</a>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="sub">提交</button>
                    <input type="hidden" name="form_id" value="0">
                    <input type="hidden" name="form_instance_id" value="0">
                    <input type="hidden" name="activity_category" value="EW_CLASS_MEETING_TABLE">
                </div> 
            </div>
        </form>
    </div>   
</div>
  
<script id="fileShowDemo" type="text/html">
<ul>
@{{#  layui.each(d, function(index, item){ }}
@{{#  if(item){ }}
    <li style="min-width: 200px;margin: 13px 5px;">
        <a style="color: #0e90d2;" target="_blank" title="@{{item.file_info.file_name}}" href="@{{item.file_info.file_dir}}@{{item.file_info.file_path}}">
            <span style="display: inline-block;max-width: 90%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">@{{item.file_info.file_name}}</span>
        </a>
        <a style="color:#74B8FF" href="@{{item.file_info.file_dir}}@{{item.file_info.file_path}}" download="@{{item.file_info.file_name}}">
            <span class="icon pear-icon pear-icon-download" style="color:#74B8FF;margin-left:10px;"></span>
        </a>
    </li>
@{{#  } }}
@{{#  }); }}
</ul>
</script>
@endsection